
# R code to extract politics subsets from the full stata
# versions of Understanding Society data

# First round of Understanding Society
rm(list = ls())
library(foreign, pos=4)
usoc1 <-
  read.dta("C:/Data/US_stata/UKDA-6614-stata12_se/stata12_se/a_indresp.dta",
   convert.dates=TRUE, convert.factors=TRUE, missing.type=TRUE,
  convert.underscore=TRUE, warn.missing.labels=TRUE)

attach(usoc1)

usoc1.extract<-usoc1 [c("pidp","a.hidp","a.pno","a.sex","a.dvage","a.mvever","a.mvyr","a.mlstat","a.ukborn","a.plbornc","a.yr2uk4","a.citzn1",
 "a.citzn2","a.citzn3","a.qfhigh","a.pacob","a.payruk","a.payruk1","a.macob","a.mayruk","a.mayruk1","a.pgprob","a.pgmrob",
 "a.natid1","a.natid2","a.natid3","a.natid4","a.natid5","a.natid6","a.natid97","a.racel","a.paid","a.spaid","a.maid","a.smaid",
 "a.britid","a.englang","a.engspk","a.spkdif","a.engtel","a.teldif","a.engread","a.readdif","a.engform","a.formdif","a.oprlg",
 "a.oprlg0","a.oprlg0ni","a.oprlg1","a.nirel","a.niact","a.oprlg2","a.oprlg3","a.mabroad","a.mnotherc","a.moveage","a.mlivedist",
 "a.lcmarm","a.lcmary4","a.mpno","a.lcmcoh","a.lcmcbm","a.lcmcby4","a.lcmspm","a.lcmspy4","a.nmar","a.lcoh","a.lncoh","a.sf1",
 "a.jbseg.dv","a.jbrgsc.dv","a.jbsect","a.jbsectpub","a.jbhrs","a.jbttwt","a.basrest","a.lvrel1","a.lvrel2","a.lvrel3",
 "a.lvrel4","a.lvrel5","a.lvrel6","a.lvrel7","a.lvrel8","a.lvrel96","a.maage","a.paage","a.parmar","a.ohch16","a.seekid","a.wekid",
 "a.finnow","a.finfut","a.vote1","a.vote2","a.vote3","a.vote4","a.vote5","a.vote6","a.envhabit1","a.envhabit2","a.envhabit3",
 "a.envhabit4","a.envhabit5","a.envhabit6","a.envhabit7","a.envhabit8","a.envhabit9","a.envhabit10","a.envhabit11","a.drive",
 "a.mobuse","a.netuse","a.nch14resp","a.nnatch","a.nadoptch","a.vote3.all","a.vote4.all","a.prfitb","a.prfitbw","a.prfitba",
 "a.country","a.marstat","a.livesp","a.livewith","a.employ","a.respf16","a.respm16","a.ioutcome","a.ivfio","a.hhresp.dv",
 "a.agegr10.dv","a.mastat.dv","a.swemwbs.dv","a.hiqual.dv","a.jbft.dv","a.jbnssec5.dv","a.fimnlabgrs.dv",
 "a.fimngrs.dv","a.gor.dv","a.urban.dv","a.indinus.xw") ]

rm(usoc1)
summary(usoc1.extract$a.sex)
usoc1.extract2<-subset(usoc1.extract,a.ioutcome=="fully productive interview     " &
 a.hhresp.dv=="All present adults interviewed")
summary(usoc1.extract2$a.sex)

usoc1.extract2$a.wave <- 1
names(usoc1.extract2)[names(usoc1.extract2)=="a.indinus.xw"] <-"a.weight.xs"

summary(usoc1.extract2)

rm("usoc1.extract")
save.image("C:\\Data\\US_stata\\UKDA-6614-stata12_se\\stata12_se\\usoc_w_a.RData")


# Second round of Understanding Society
rm(list = ls())
library(foreign, pos=4)
usoc2 <-
  read.dta("C:/Data/US_stata/UKDA-6614-stata12_se/stata12_se/b_indresp.dta",
  convert.dates=TRUE, convert.factors=TRUE, missing.type=TRUE,
  convert.underscore=TRUE, warn.missing.labels=TRUE)

attach(usoc2)

usoc2.extract<-usoc2 [c("pidp","b.hidp","b.pno","b.sex","b.dvage","b.mvever","b.mvyr","b.mlstat","b.ukborn","b.plbornc","b.yr2uk4","b.citzn1",
 "b.citzn2","b.citzn3","b.qfhigh","b.pacob","b.macob",
 "b.natid1","b.natid2","b.natid3","b.natid4","b.natid5","b.natid6","b.natid97","b.racel",
 "b.oprlg",
 "b.oprlg0","b.oprlg0ni","b.oprlg1","b.nirel","b.niact",
 "b.jbseg.dv","b.jbrgsc.dv","b.jbsect","b.jbsectpub","b.jbhrs","b.jbttwt","b.basrest",
 "b.finnow","b.finfut","b.vote1","b.vote2","b.vote3","b.vote4","b.vote5","b.vote6","b.vote7","b.vote8",
 "b.volun","b.volfreq","b.volhrs","b.chargv","b.charfreq","b.charam",
 "b.hubuys","b.hufrys","b.humops","b.huiron","b.hupots","b.hudiy","b.husits","b.huboss","b.howlng","b.drive",
 "b.mobuse","b.netuse","b.nch14resp","b.nnatch","b.nadoptch","b.vote3.all","b.vote4.all","b.prfitb","b.prfitbw","b.prfitba",
 "b.country","b.marstat","b.livesp","b.livewith","b.employ","b.respf16","b.respm16","b.ioutcome","b.ivfio","b.hhresp.dv",
 "b.agegr10.dv","b.mastat.dv","b.hiqual.dv","b.jbft.dv","b.jbnssec5.dv","b.fimnlabgrs.dv",
 "b.fimngrs.dv","b.gor.dv","b.urban.dv","b.indinus.xw") ]


rm(usoc2)
levels(usoc2.extract$b.ioutcome)
levels(usoc2.extract$b.hhresp)


summary(usoc2.extract$b.sex)
usoc2.extract2<-subset(usoc2.extract,b.ioutcome=="fully productive interview" &
 b.hhresp.dv=="All present adults interviewed")
summary(usoc2.extract2$b.sex)

usoc2.extract2$b.wave <- 2
names(usoc2.extract2)[names(usoc2.extract2)=="b.indinus.xw"] <-"b.weight.xs"

summary(usoc2.extract2)
rm("usoc2.extract")
save.image("C:\\Data\\US_stata\\UKDA-6614-stata12_se\\stata12_se\\usoc_w_b.RData")


# Third round of Understanding Society
rm(list = ls())
library(foreign, pos=4)
usoc3 <-
  read.dta("C:/Data/US_stata/UKDA-6614-stata12_se/stata12_se/c_indresp.dta",
  convert.dates=TRUE, convert.factors=TRUE, missing.type=TRUE,
  convert.underscore=TRUE, warn.missing.labels=TRUE)

attach(usoc3)

usoc3.extract<-usoc3 [c("pidp","c.hidp","c.pno","c.sex","c.dvage","c.mvever","c.mvyr","c.mlstat","c.ukborn","c.plbornc","c.yr2uk4","c.citzn1",
 "c.citzn2","c.citzn3","c.qfhigh","c.pacob","c.macob",
 "c.natid1","c.natid2","c.natid3","c.natid4","c.natid5","c.natid6","c.natid97","c.racel",
 "c.oprlg","c.oprlg0","c.oprlg0ni","c.oprlg1","c.nirel","c.niact",
 "c.jbseg.dv","c.jbrgsc.dv","c.jbsect","c.jbsectpub","c.jbhrs","c.jbttwt","c.basrest",
 "c.finnow","c.finfut","c.vote1","c.vote2","c.vote3","c.vote4","c.vote5","c.vote6",
 "c.poleff1","c.poleff2","c.poleff3","c.poleff4",
 "c.newsmain","c.paperm2","c.tvm2","c.netm2","c.tvhours","c.drive",
 "c.mobuse","c.netuse","c.nch14resp","c.nnatch","c.nadoptch","c.vote3.all","c.vote4.all","c.prfitb","c.prfitbw","c.prfitba",
 "c.country","c.marstat","c.livesp","c.livewith","c.employ","c.respf16","c.respm16","c.ioutcome","c.ivfio","c.hhresp.dv",
 "c.agegr10.dv","c.mastat.dv","c.hiqual.dv","c.jbft.dv","c.jbnssec5.dv","c.fimnlabgrs.dv",
 "c.fimngrs.dv","c.gor.dv","c.urban.dv","c.indinub.xw") ]


rm(usoc3)
levels(usoc3.extract$c.ioutcome)
levels(usoc3.extract$c.hhresp)


summary(usoc3.extract$c.sex)
usoc3.extract2<-subset(usoc3.extract,c.ioutcome=="tf_iout11" &
 c.hhresp.dv=="All present adults interviewed")
summary(usoc3.extract2$c.sex)

usoc3.extract2$c.wave <- 3
names(usoc3.extract2)[names(usoc3.extract2)=="c.indinub.xw"] <-"c.weight.xs"

summary(usoc3.extract2)
rm("usoc3.extract")
save.image("C:\\Data\\US_stata\\UKDA-6614-stata12_se\\stata12_se\\usoc_w_c.RData")



# Merge the three files
# clear the workspace
rm(list = ls())

# load the three component files
load("C:\\Data\\US_stata\\UKDA-6614-stata12_se\\stata12_se\\usoc_w_a.RData")
load("C:\\Data\\US_stata\\UKDA-6614-stata12_se\\stata12_se\\usoc_w_b.RData")
load("C:\\Data\\US_stata\\UKDA-6614-stata12_se\\stata12_se\\usoc_w_c.RData")

attach(usoc1.extract2)
attach(usoc2.extract2)
attach(usoc3.extract2)

newd<-merge(usoc1.extract2,usoc2.extract2,by="pidp")
newd_123<-merge(newd,usoc3.extract2,by="pidp")
rm("usoc1.extract2")
rm("usoc2.extract2")
rm("usoc3.extract2")
rm("newd")
save.image("C:\\Data\\US_stata\\UKDA-6614-stata12_se\\stata12_se\\usoc_w_abc.RData")




